package com.woman.beautylive.presentation.ui.room.create;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.umeng.analytics.MobclickAgent;
import com.woman.beautylive.BeautyLiveApplication;
import com.woman.beautylive.R;
import com.woman.beautylive.data.bean.LoginInfo;
import com.woman.beautylive.data.bean.ThemBean;
import com.woman.beautylive.data.repository.SourceFactory;
import com.woman.beautylive.domain.LocalDataManager;
import com.woman.beautylive.presentation.ui.base.BaseActivity;
import com.woman.beautylive.presentation.ui.base.recycler.SimpleRecyclerAdapter;
import com.woman.beautylive.presentation.ui.base.recycler.SimpleRecyclerHolder;
import com.woman.beautylive.presentation.ui.room.RoomActivity;
import com.woman.beautylive.presentation.ui.room.publish.PublishFragment;
import com.woman.beautylive.util.Const;
import com.woman.beautylive.util.L;
import com.woman.beautylive.util.share.ShareHelper;
import com.yolanda.nohttp.Headers;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import rx.functions.Action1;

/**
 * @author Muyangmin
 * @since 1.0.0
 */
public class CreateRoomActivity extends BaseActivity implements CreateRoomInterface {

    //    标题
    private EditText edtLiveTitle;
    //    分享按钮
    private ImageButton imgbtnWeibo, imgbtnWechatCircle, imgbtnWeChat, imgbtnQQ, imgbtnQzone;
    private List<ImageButton> listInitShare;
    private PopupWindow popupWindowInitShare;
    private TextView tvInitShareTips;
    private int mInitShareIndex = -1;        //初始创建时的分享平台
    private Platform mPlatform;
    private TextView changes;

    private static final String GETCTIY="city";
    private static final String GETPROVINCE="province";
    private String mCity;
    private String mProvince;
    private CreateRoomShareHelper shareHelper;

    private ImageButton imgbtnLocking, imgbtnUnlocking;
    private Button btnStartLive;

    private CreateRoomPresenter presenter;
    private String mPushAddress;
    private Dialog mLoadingDialog;
    private int change;

//    城市
    private TextView mCityTv;
    //    话题输入框的值
    private String mTitleThem;
    //    话题列表
    private List<ThemBean.Topic> topicsList;
    //    文本监听的
    private CharSequence temp;//监听前的文本
    private int editStart;//光标开始位置
    private int editEnd;//光标结束位置
    private final int charMaxNum = 16;
    //    屏幕高度 状态栏高度
    private int stateh, xunih;
//    记录输入前的标题字数
    private int editbefor;
    private TextView roomToptx;
    public static Intent createIntent(Context context,String city,String province) {
        Intent intent= new Intent(context, CreateRoomActivity.class);
        intent.putExtra(GETCTIY,city);
        intent.putExtra(GETPROVINCE,province);
        return intent;
    }

    @Override
    protected void parseIntentData(Intent intent, boolean isFromNewIntent) {
        super.parseIntentData(intent, isFromNewIntent);
        mCity=intent.getStringExtra(GETCTIY);
        mProvince=intent.getStringExtra(GETPROVINCE);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_create_room;
    }

    public static int changed;
    @Override
    protected void findViews(Bundle savedInstanceState) {
        presenter = new CreateRoomPresenter(this);
        roomToptx=$(R.id.create_room_top_tx);
        imgbtnLocking = $(R.id.room_create_imgbtn_locking);
        imgbtnUnlocking = $(R.id.room_create_imgbtn_unlocking);
        mCityTv=$(R.id.create_room_top_city);
        imgbtnQQ = $(R.id.room_create_imgbtn_share_qq);
        imgbtnQzone = $(R.id.room_create_imgbtn_share_qzone);
        imgbtnWeChat = $(R.id.room_create_imgbtn_share_wechat);
        imgbtnWechatCircle = $(R.id.room_create_imgbtn_share_wechat_circle);
        imgbtnWeibo = $(R.id.room_create_imgbtn_share_weibo);
        changes = $(R.id.change);

        listInitShare = new ArrayList<>(5);
        Collections.addAll(listInitShare, imgbtnQQ, imgbtnQzone, imgbtnWeChat,
                imgbtnWechatCircle, imgbtnWeibo);
        if (mCity!=null) {
            mCityTv.setText(mCity);
        }else {
            mCityTv.setText(getString(R.string.city_tip));
        }
        changes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (changes.getText().toString().equals("横屏模式")){
                    changes.setText("竖屏模式");
                    change=1;
                }else if(changes.getText().toString().equals("竖屏模式")) {
                    changes.setText("横屏模式");
                    change=2;
                }
                changed=change;
            }
        });

        LoginInfo loginInfo = LocalDataManager.getInstance().getLoginInfo();
//        mInitShareIndex=1;
//        mPlatform=new Wechat(this);
//        listInitShare.get(mInitShareIndex).setSelected(true);

        shareHelper = new CreateRoomShareHelper(this, loginInfo.getCurrentRoomNum(), loginInfo
                .getNickname(),loginInfo.getAvatar(), new ShareHelper.DefaultShareListener() {
            @Override
            public void onCancel(Platform platform, int i) {
                super.onCancel(platform, i);
                enterLiveRoom();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                super.onError(platform, i, throwable);
                enterLiveRoom();
            }

            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                super.onComplete(platform, i, hashMap);
                enterLiveRoom();
            }
        });

        subscribeInitShare(imgbtnQQ, 0, new QQ(this), getString(R.string.share_channel_qq));
        subscribeInitShare(imgbtnQzone, 1, new QZone(this), getString(R.string.share_channel_qzone));
        subscribeInitShare(imgbtnWeChat, 2, new Wechat(this), getString(R.string.share_channel_wechat));
        subscribeInitShare(imgbtnWechatCircle, 3, new WechatMoments(this), getString(R.string
                .share_channel_wechat_friend_circle));
        subscribeInitShare(imgbtnWeibo, 4, new SinaWeibo(this), getString(R.string.share_channel_weibo));
        edtLiveTitle = $(R.id.room_edt_title);
        edtLiveTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                temp = s;
                editbefor=s.length();
                Log.i("mrl","输入前"+editbefor);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("mrl", s.toString() + " " + start + " " + before + "这个是切换");

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("mrl", "输入后"+s+" "+temp.length()+" "+s.length());
                /** 得到光标开始和结束位置 ,超过最大数后记录刚超出的数字索引进行控制 */
                editStart = edtLiveTitle.getSelectionStart();
                editEnd = edtLiveTitle.getSelectionEnd();
//                字数限制
//                if (temp.length() > charMaxNum) {
//                    toastShort("你输入的字数已经超过了限制！");
//                    s.delete(editStart - 1, editEnd);
//                    int tempSelection = editStart;
//                    edtLiveTitle.setText(s);
//                    edtLiveTitle.setSelection(tempSelection);
//                }
//      这里判断是删除操作还是输入操作
                if (editbefor<s.length()) {
                    //                防止删除到最后一个的时候里面判断出现数组越界
                    if (s.length() != 0 ) {
//                    如果只有最后一位
                        if (s.length() == 1) {
                            if (s.charAt(s.length() - 1) == '#') {
                                s.delete(0, editEnd);
                                startThemPopupWindow();
                            }
                        } else if (editStart==s.length()){
                            if (s.charAt(s.length() - 1) == '#') {
                                s.delete(editStart - 1, editEnd);
                                startThemPopupWindow();
                            }
                        }
                    }
                }
            }
        });
        edtLiveTitle.setSelection(edtLiveTitle.getText().length());
        btnStartLive = $(R.id.room_create_btn_start_live);

//        开始直播
        subscribeClick(btnStartLive, new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                if (TextUtils.isEmpty(mPushAddress)) {
                    mLoadingDialog = showLoadingDialog();
                } else {
                    checkAndStartPublish();
                }
            }
        });

        subscribeClick($(R.id.room_create_btn_close), new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                finish();
            }
        });

        subscribeClick($(R.id.room_live_create_theme), new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                startThemPopupWindow();
            }
        });

        presenter.generatePushStreaming();
        presenter.getThemBean();
        getViewPagerJson();
    }

    private void checkAndStartPublish() {
        if ((!TextUtils.isEmpty(mPushAddress))
                && (mLoadingDialog == null || (!mLoadingDialog.isShowing()))) {
//            有开启某个平台的分享
            if (mInitShareIndex == 2) {
                L.e("________", "________222" + mInitShareIndex);
                LoginInfo loginInfo = LocalDataManager.getInstance().getLoginInfo();
                String shareTitleUrl = getString(R.string.share_room_url,Const.MAIN_HOST_TEST, loginInfo.getCurrentRoomNum());
                Log.i("mrl", "这个是分享的地址" + R.string.share_room_url + "        " + Const.MAIN_HOST_TEST + "   " + loginInfo.getCurrentRoomNum());
                String shareText = getString(R.string.share_room_text, loginInfo.getNickname() + edtLiveTitle.getText().toString());
                Wechat.ShareParams params = new Wechat.ShareParams();
                params.setTitleUrl(shareTitleUrl);
                params.setTitle(getString(R.string.share_title));
                params.setText(shareText);
                params.setImageUrl(SourceFactory.wrapPath(loginInfo.getAvatar()));
                params.setShareType(Platform.SHARE_WEBPAGE);
                params.setUrl(shareTitleUrl);
                Platform qzone = ShareSDK.getPlatform(Wechat.NAME);
                qzone.setPlatformActionListener(new ShareHelper.DefaultShareListener() {
                    @Override
                    public void onCancel(Platform platform, int i) {
                        super.onCancel(platform, i);
                        L.e("_________", "_______onCancel");
                        enterLiveRoom();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        super.onError(platform, i, throwable);
                        enterLiveRoom();
                    }

                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        super.onComplete(platform, i, hashMap);
                        enterLiveRoom();
                    }
                }); // 设置分享事件回调
                qzone.share(params);
            }

            if (mInitShareIndex != -1) {
                shareHelper.share(mPlatform);
            } else {
                enterLiveRoom();
            }
        }
    }

    @Override
    protected void onResume() {
        MobclickAgent.onPageStart("开播前设置分享标题");
        super.onResume();
    }

    @Override
    protected void onPause() {
        MobclickAgent.onPageEnd("开播前设置分享标题");
        super.onPause();
    }

    //   开启直播
    private void enterLiveRoom() {

        //Simply gone
//        if (!TextUtils.isEmpty(edtLiveTitle.getText().toString().trim())) {
        LoginInfo loginInfo = LocalDataManager.getInstance().getLoginInfo();
        if (mCity==null) {
            presenter.postCreatRoom(loginInfo.getToken(), edtLiveTitle.getText().toString(), loginInfo.getCurrentRoomNum(), "火星","火星");
        }else {
            mProvince=mProvince.replaceAll("省","");
            presenter.postCreatRoom(loginInfo.getToken(), edtLiveTitle.getText().toString(), loginInfo.getCurrentRoomNum(), mCity,mProvince);
        }
        getViewPagerJsons(loginInfo.getToken(),edtLiveTitle.getText().toString());
        startActivity(RoomActivity.createIntent(CreateRoomActivity.this, RoomActivity
                .TYPE_PUBLISH_LIVE, loginInfo.getCurrentRoomNum(), loginInfo
                .getUserId(), PublishFragment.createArgs(mPushAddress)));
        finish();
//        } else {
//            toastShort("标题为空");
//        }
    }

    @Override
    protected void init() {

    }

    @Override
    public void onPushStreamReady(String address) {
        mPushAddress = address;
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            checkAndStartPublish();
        }
    }

    //    得到话题列表对象
    @Override
    public void onThemBean(ThemBean themBean) {
        topicsList = themBean.getTopic();
        Log.i("mrl",topicsList+"");
    }

    @Override
    public void getCreamRoom(String tost) {
        toastShort(tost);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unsubscribeTasks();
    }

    private void subscribeInitShare(View view,
                                    final int index,
                                    final Platform platform,
                                    final String shareChannel) {
        subscribeClick(view, new Action1<Void>() {

            @Override
            public void call(Void aVoid) {
                if (popupWindowInitShare == null) {
                    @SuppressLint("InflateParams")
                    View contentView = LayoutInflater.from(CreateRoomActivity.this).inflate(R.layout
                            .popup_room_create_share, null);
                    tvInitShareTips = $(contentView, R.id.room_create_tv_share_tip);
                    popupWindowInitShare = new PopupWindow(contentView, ViewGroup.LayoutParams
                            .WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                }
                if (popupWindowInitShare.isShowing()) {
                    popupWindowInitShare.dismiss();
                }
                /**
                 * 1.所有的都没被选中：设置点击项selected为true。
                 * 2.有一项选中，现在点击的是另一个：设置之前的为false,现在的为true。
                 * 3.有一项选中，现在又点了这个：设置为false。
                 *
                 * 所以：
                 * Step1 只要有一项选中，直接设置为false。
                 * Step2 接下来判断点击项，如果是自己且之前为选中状态则将选中标记置为-1，表示没有选中任何项；
                 * 否则将点击项置为true且展示popWindow。
                 */
                boolean isSelfSelected = false;

                //Step1
                if (mInitShareIndex != -1) {
                    listInitShare.get(mInitShareIndex).setSelected(false);
                    isSelfSelected = mInitShareIndex == index;
                }

                //Step2
                // 仅在不相等时才赋值和设置选中，因可以取消所有选项
                if (isSelfSelected) {
                    mInitShareIndex = -1;
                } else {
                    mInitShareIndex = index;
                    mPlatform = platform;

                    listInitShare.get(mInitShareIndex).setSelected(true);
                    tvInitShareTips.setText(getString(R.string.room_live_create_share_tips,
                            shareChannel));
                    L.i(LOG_TAG, "Showing pop window!");
//                    popupWindowInitShare.showAtLocation(listInitShare.get(mInitShareIndex),
//                            Gravity.TOP, 0, 0);
                    showPopupWindowAboveButton(popupWindowInitShare, listInitShare.get(mInitShareIndex));
                    tvInitShareTips.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (popupWindowInitShare != null && popupWindowInitShare.isShowing()) {
                                popupWindowInitShare.dismiss();
                            }
                        }
                    }, 3000);
                }
            }
        });
    }

    protected final void showPopupWindowAboveButton(@NonNull PopupWindow window, @NonNull View
            anchor) {
        View popupView = window.getContentView();
        //执行一次measure，避免第一次无法获取位置的问题。
        popupView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = popupView.getMeasuredWidth();
        int popupHeight = popupView.getMeasuredHeight();
        int[] location = new int[2];
        anchor.getLocationOnScreen(location);
        //To fix the bug of inability for automatically dismiss pop window when touch outside
        window.setBackgroundDrawable(new BitmapDrawable());
        window.setOutsideTouchable(true);
        window.showAtLocation(anchor, Gravity.NO_GRAVITY, (location[0] + anchor.getWidth() / 2)
                        - popupWidth / 2,
                location[1] - popupHeight);
    }


    //  话题
    private PopupWindow mPopupWindow;
    private View popuView;
    private TextView popuClose, popuListNull;
    private RecyclerView popuList;
    private EditText popuET;

    private void startThemPopupWindow() {
        if (mPopupWindow == null) {
            getXuNiDpi();
//            getWindowKeyBrodH();
            LayoutInflater relativeLayout = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            popuView = relativeLayout.inflate(R.layout.start_theme_pop_layout, null);
            popuET = (EditText) popuView.findViewById(R.id.start_theme_et);
            popuClose = (TextView) popuView.findViewById(R.id.start_theme_close);
            popuList = (RecyclerView) popuView.findViewById(R.id.them_list);
            popuListNull = (TextView) popuView.findViewById(R.id.them_empty);
            popuList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            if (topicsList == null) {
                popuList.setVisibility(View.GONE);
                popuListNull.setVisibility(View.VISIBLE);
            } else {
                ThemAdapter adapter = new ThemAdapter(topicsList);
                popuList.setAdapter(adapter);
            }
            mPopupWindow = new PopupWindow(popuView,
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            popuClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    closepopwindow(popuView);
                    mPopupWindow.dismiss();
                }
            });
            popuET.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (event!=null) {
                        Log.i("mrl", actionId + " " + event.getKeyCode() + "这个哦");
                    }
                    if (actionId == EditorInfo.IME_ACTION_DONE || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                            edtLiveTitle.getText().append("#"+v.getText()+"##");
                        Log.i("mrl",edtLiveTitle.getText().toString()+"当前文本");
                            popuET.getText().clear();
                            mPopupWindow.dismiss();
                        return true;
                    }
                    return false;
                }
            });
            // 使其聚集
            mPopupWindow.setFocusable(true);
            // 设置允许在外点击消失
            mPopupWindow.setOutsideTouchable(true);
            // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
            mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);
            if (xunih==0){
                mPopupWindow.showAsDropDown(roomToptx,0,-stateh);
            }else {
                mPopupWindow.showAsDropDown(roomToptx, 0, -(xunih - stateh));
            }
//            startpopwindow(popuView);
        } else {
            if (xunih==0){
                mPopupWindow.showAsDropDown(roomToptx,0,-stateh);
            }else {
                mPopupWindow.showAsDropDown(roomToptx, 0, -(xunih - stateh));
            }
//            startpopwindow(popuView);
        }
    }


    // 话题列表适配器
    private class ThemAdapter extends SimpleRecyclerAdapter<ThemBean.Topic, RecommendHolder> {
        public ThemAdapter(List<ThemBean.Topic> anchorSummaries) {
            super(anchorSummaries);
        }

        @Override
        protected int getItemLayoutId(int viewType) {
            return R.layout.start_them_list_item;
        }

        @NonNull
        @Override
        protected RecommendHolder createHolder(View view) {
            return new RecommendHolder(view);
        }
    }

    private class RecommendHolder extends SimpleRecyclerHolder<ThemBean.Topic> {

        private TextView mChatTitile, mChartNumber;

        public RecommendHolder(View itemView) {
            super(itemView);
            mChatTitile = (TextView) itemView.findViewById(R.id.item_them_tv);
            mChartNumber = (TextView) itemView.findViewById(R.id.item_them_number);
        }

        @Override
        public void displayData(final ThemBean.Topic data) {
            mChatTitile.setText(data.getTopic_title());
            mChartNumber.setText(data.getTopic_num() + " 直播");
            RxView.clicks(itemView)
                    .throttleFirst(Const.VIEW_THROTTLE_TIME, TimeUnit.MILLISECONDS)
                    .subscribe(new Action1<Void>() {
                        @Override
                        public void call(Void aVoid) {
                            edtLiveTitle.getText().append("#"+data.getTopic_title()+"##");
                            mPopupWindow.dismiss();
                        }
                    });
        }
    }


    int PAGER_JSON = 1;

    //Mrl L
    public void getViewPagerJson() {
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(Const.NEW_URL + "/OpenAPI/V1/Topic/getTopic", RequestMethod.GET);
        BeautyLiveApplication.getRequestQueue().add(PAGER_JSON, request, ViewPagerOnResponse);
    }

    private OnResponseListener<JSONObject> ViewPagerOnResponse = new OnResponseListener<JSONObject>() {
        @Override
        public void onStart(int i) {
            L.i("lll", "开始了");
        }

        @Override
        public void onSucceed(int i, Response<JSONObject> response) {
            if (i == PAGER_JSON) {// 判断what是否是刚才指定的请求
                //                {"code":0,"msg":"ok","data":"320200"}这个是什么
                // 请求成功
                JSONObject result = response.get();// 响应结果
                Log.i("lll", result + " ");
                // 响应头
                Headers headers = response.getHeaders();
                headers.getResponseCode();// 响应码
                response.getNetworkMillis();// 请求花费的时间
            }
        }

        @Override
        public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
            L.i("lll", "请求失败了哦" + s);
        }

        @Override
        public void onFinish(int i) {
            L.i("lll", "viewpager结束了");
        }
    };

    //Mrl L
    public void getViewPagerJsons(String token,String title) {
        Log.i("lll",token+" "+ title+"这个进入了请求");
        Request<JSONObject> request = NoHttp.createJsonObjectRequest(Const.NEW_URL + "room/createRoom", RequestMethod.GET);
        request.add("token",token);
        request.add("title",title);
        BeautyLiveApplication.getRequestQueue().add(2, request, ViewPagerOnResponse1);
    }

    private OnResponseListener<JSONObject> ViewPagerOnResponse1 = new OnResponseListener<JSONObject>() {
        @Override
        public void onStart(int i) {
            L.i("lll", "开始了");
        }

        @Override
        public void onSucceed(int i, Response<JSONObject> response) {
            if (i == 2) {// 判断what是否是刚才指定的请求
                //                {"code":0,"msg":"ok","data":"320200"}这个是什么
                // 请求成功
                JSONObject result = response.get();// 响应结果
                Log.i("lll", result + " ");
                // 响应头
                Headers headers = response.getHeaders();
                headers.getResponseCode();// 响应码
                response.getNetworkMillis();// 请求花费的时间
            }
        }

        @Override
        public void onFailed(int i, String s, Object o, Exception e, int i1, long l) {
            L.i("lll", "请求失败了哦" + s);
        }

        @Override
        public void onFinish(int i) {
            L.i("lll", "viewpager结束了");
        }
    };

    private void getXuNiDpi() {   int dpi = 0;
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        @SuppressWarnings("rawtypes")
        Class c;
        try {
            c = Class.forName("android.view.Display");
            @SuppressWarnings("unchecked")
            Method method = c.getMethod("getRealMetrics",DisplayMetrics.class);
            method.invoke(display, dm);
            dpi=dm.heightPixels;
        }catch(Exception e){
            e.printStackTrace();
        }
        xunih=dpi-getWindowManager().getDefaultDisplay().getHeight();
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        stateh=rect.top;

        Display disp = this.getWindowManager().getDefaultDisplay();
        Point outP = new Point();
        disp.getSize(outP);

        Log.i("mrl",xunih+"这个是虚拟按键高度"+ rect.top+"这个是状态栏高度"+"这个不知道"+outP.y);
    }
}
